package com.ntu.rent.common.util;

import java.lang.management.ManagementFactory;
import java.util.HashSet;
import java.util.Set;

public class UUID {
	
	private long time;
	private static Object mutex = new Object();
	private static long lastTime = System.currentTimeMillis();
	private static long DELAY = 10L;
	private static String jvmName = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
	
	private UUID(){
		synchronized (mutex) {
			boolean flag = false;
			while (!flag) {
				time = System.currentTimeMillis();
				if (time < lastTime + DELAY) {
					try {
						Thread.currentThread();
						Thread.sleep(DELAY);
					} catch (InterruptedException interruptedexception) {
					}
				} else {
					lastTime = time;
					flag = true;
				}
			}
		}
	}
	/**通过系统时间获得UUID**/
	public static String getUUID(){
		String uuid = new UUID().toString();
		return uuid.toUpperCase();
	}
	
	public static String[] getUUIDs(int size)
	{
		String[] ids = new String[size];
		String id = getUUID();
		for (int i = 0; i < ids.length; i++)
		{
			ids[i] = id+""+i;
		}
		return ids;
	}
	
	public String toString() {
		return "AZ"+jvmName+Long.toString(time, 16);
	}
	public static void main(String[] args) {
		Set<String> aa = new HashSet<String>();
		String[] ids = getUUIDs(30);
		for (int i = 0; i < 30; i++) {
			aa.add(ids[i]);
			System.out.println(ids[i]);
		}
		System.out.println(aa.size());
	}
}
